package com.search.examples;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.search.entity.BorrowBook;
import com.search.tools.CaffeineTool;
import com.search.tools.MD5EncryptTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
public class ZheJiangSearch {

    private  static  String url = "http://ulib.interlib.cn/tcshop/1111/product/search.html?cmdAct=search&key=";
    private  String entityStr = "{\"libcode\":\"STL50\",\"channel\":\"yuntu-wx-mini\",\"timestamp\":shijian,\"salt\":448928,\"sign\":\"qianming\",\"bizParam\":{\"pageNo\":1,\"pageSize\":15,\"keyword\":\"shuhao\",\"holdingStatus\":0,\"searchType\":1}}";

    private static String detailHost = "http://ulib.interlib.cn/";


    private int getBookNum(String detailUrl){
        Connection connection = Jsoup.connect(detailHost+detailUrl);
        Document document = null;
        try {
            document = connection.post();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Element stockEle = document.getElementById("stock_span");
        try {
            if("无复本可借".equals(stockEle.text().trim())){
                return 0;
            } else if ("库存充裕".equals(stockEle.text().trim())) {
                return 100;
            } else if(stockEle.text().trim().startsWith("可借复本")){
                return Integer.parseInt(stockEle.text().trim().substring(stockEle.text().trim().length()-1));
            }
        } catch (NumberFormatException e) {
            return -1000;
        } finally {
        }

        return 1000;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        //post("url");
    }
    //0没书 1有书 -1有书被别人借走了 -2返回数据结构异常
    public BorrowBook search2(BorrowBook borrowBook){

       // JSONObject result = post(borrowBook.getIsbn());
        JSONObject result = null;
        int num = 0;
        if(null!=result){
            int code = result.getInteger("code");
            boolean isSuccess = result.getBoolean("success");
            if(isSuccess && 0==code){
                JSONObject data = result.getJSONObject("data");
                if(null!=data){
                    int total = data.getInteger("total");
                    log.debug("搜索到数量"+total);
                    if(total>1){
                        log.debug("怎么这么多"+result.toString());
                    }
                    JSONArray jsonArray = data.getJSONArray("books");
                    if(null!=jsonArray && jsonArray.size()>0){
                        JSONObject book = (JSONObject) jsonArray.get(0);
                        int holding = book.getInteger("availableNumber");
                        if(holding>0){
                            num= 1;
                        }else {
                            num= -1;
                        }

                    }else {
                        num= 0;
                    }
                }else{
                    num= -2;
                }
            }
        }else {
            num= -2;
        }
        borrowBook.setHolding(num);
        return borrowBook;
    }

    BorrowBook search(BorrowBook borrowBook){

        JSONObject jsonObject = new JSONObject();
        int num = 0;
        Connection connection = Jsoup.connect(url+borrowBook.getIsbn());
        Document document = null;
        try {
            document = connection.post();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Elements bookLists = document.getElementsByClass("bookList");
        if(null!=bookLists && bookLists.size()>0){
            Element bookList = bookLists.get(0);
            Elements lis = bookList.getElementsByTag("li");
            for(Element li:lis){
                Elements bkType02s = li.getElementsByClass("bkType02");
                if(null!=bkType02s && bkType02s.size()>0){
                    Element bkType02 = bkType02s.get(0);
                    if("纸质书".equals(bkType02.text())){
                        Elements bookLibs = li.getElementsByClass("bookLib");
                        if(null!=bookLibs && bookLibs.size()>0){
                            Element bookLib = bookLibs.get(0);
                            if("浙江图书馆".equals(bookLib.text())){
                                String detailUrl = li.getElementsByTag("a").get(0).attr("href");
                                borrowBook.setHolding(getBookNum(detailUrl));
                            }
                        }
                    }
                }
            }
        }

        return borrowBook;
    }


}
